import LanguageMng from "../../Managers/LanguageMng";
import LanguageMngTs from "../../Managers/LanguageMngTs";

cc.Class({
    extends: cc.Component,

    properties: {
        label:{
            default:null,
            type:cc.Label
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.stopCb = this._onHideGameWaitNotice.bind(this)
        cc.find("Canvas").on("game_count_down_stop",this.stopCb,this)
    },

    _onHideGameWaitNotice:function () {
        this.unschedule(this.timeCallback)
        this.node.destroy()
    },

    start () {

    },

    onDestroy(){
        // cc.find("Canvas").off("game_count_down_stop",this.stopCb,this)
    },

    // update (dt) {},

    startGameCountDown:function (maxTime) {
        if(this.timeCallback){
            this.unschedule(this.timeCallback)
            this.timeCallback = null
        }

        this.maxTime = maxTime

        let oriStr = LanguageMngTs.getSentence(313) //The game will start in %time second(s)
        let showTime = parseInt(this.maxTime)
        let realStr = cc.gg.baseUtil.getNewStringByReplace(oriStr,"%time",showTime)

        this.label.string = realStr

        this.timeCallback = function (dt) {
            if(this.maxTime === 0){
                this.unschedule(this.timeCallback)
                this.node.destroy()
                return
            }
            this.maxTime--

            let oriStr = LanguageMngTs.getSentence(313) //The game will start in %time second(s)
            let showTime2 = parseInt(this.maxTime)
            let realStr = cc.gg.baseUtil.getNewStringByReplace(oriStr,"%time",showTime2)
            this.label.string = realStr
        }.bind(this)
        this.schedule(this.timeCallback, 1);
    },

    updateGameCountDown:function (nowLeftTime) {
        nowLeftTime = Math.ceil(nowLeftTime)
        if(nowLeftTime === 0){
            this.node.destroy()
            return
        }
        let str = LanguageMng.getSentence(197)
        let mainNameHolder = "%num"
        var regMain = new RegExp( mainNameHolder.toString() , "g" )
        str = str.replace(regMain,nowLeftTime)
        this.label.string = str
    },
});
